package com.ml.doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.ml.doctor.R;
import com.ml.doctor.bean.UserInfoBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EditInfoActivity extends BaseActivity {
    @BindView(R.id.et_edit)
    EditText etEdit;
    String type;

    UserInfoBean userInfoBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_layout);
        ButterKnife.bind(this);
        setRightText("完成");
        setTopTitle("编辑");
        initData();
    }


    private void initData() {
        type = getStringParam("type");
        userInfoBean = getSerializableParam("info", UserInfoBean.class);
    }

    @Override
    protected void onRightTextClick() {
        super.onRightTextClick();
        commit();
    }

    private void commit() {
        if (TextUtils.isEmpty(etEdit.getText().toString())) {
            ToastUtil.showShort(this, "不能为空");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserId() + "");
        map.put("doctername", userInfoBean.doctername + "");
        map.put("hosname", userInfoBean.hosname + "");
        map.put("adds", userInfoBean.adds + "");
        map.put("duty", userInfoBean.duty + "");
        map.put("department", userInfoBean.department + "");
        map.put("card", userInfoBean.card + "");
        map.put("rankid", userInfoBean.rankid + "");
        map.put("gat", userInfoBean.gat + "");
        map.put("pend", userInfoBean.pend + "");
        map.put("pro", userInfoBean.pro + "");
        map.put("apply_amount", userInfoBean.apply_amount + "");
        map.put("service_amount", userInfoBean.service_amount + "");
        map.put("docter_photo", userInfoBean.docter_photo + "");
//        map.put("pwd", LocalShared.getInstance(this).getUserId() + "");
        map.put(type, etEdit.getText().toString());
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/doedit", map, UserInfoBean.class, new NetworkManager.SuccessCallback<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean response) {
                ToastUtil.showShort(EditInfoActivity.this, "提交审核成功");
                //saveToLocal(response);
                Intent i = new Intent();
                i.putExtra("response", response);
                setResult(RESULT_OK, i);
                finish();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(EditInfoActivity.this, message);

            }
        });
    }

    private void saveToLocal(UserInfoBean response) {
        LocalShared.getInstance(this).setUserId(response.docterid);
        LocalShared.getInstance(this).setUserNick(response.doctername);
        LocalShared.getInstance(this).setUserPhone(response.tel);
        LocalShared.getInstance(this).setUserPro(response.pro);
        LocalShared.getInstance(this).setPhoto(response.docter_photo);
    }
}
